# TODO 导入base模块
import time

from common.base import Base, By
from page.reception.login_page import LoginPage
from common.functions import browser_operation

class ReceivingAddressPages(Base):

    # TODO 点击：收货地址 按钮：class="curs"/html/body/div[6]/div[1]/div/div/div/div/a[4]
    def btn_receiving_address(self):
        self.click((By.XPATH,'/html/body/div[6]/div[1]/div/div/div/div/a[4]'))

    # TODO 定位：*请选择省 下拉定位：id="selProvinces_0"
    def select_province(self, text):
        self.select_box((By.ID, 'selProvinces_0'), text)

    # TODO 定位：*请选择市 下拉定位：id="selCities_0"
    def select_city(self,text):
        self.select_box((By.ID, 'selCities_0'), text)

    # TODO 定位：*请选择区,下拉定位：id="selDistricts_0"
    def select_district(self, text):
        self.select_box((By.ID, 'selDistricts_0'), text)

    # TODO 定位：*收货人姓名：class="inputBg"
    def consignee_name(self,cons_name):
        self.send_key((By.CLASS_NAME,'inputBg'),cons_name)

    # TODO 定位：*详细地址：name="address"
    def detailed_addr(self,det_addr):
        self.send_key((By.NAME, 'address'),det_addr)

    # TODO 定位：电话：name="tel"
    def telephone(self,tel):
        self.send_key((By.NAME, 'tel'),tel)

    # TODO 定位：*电子邮件地址：name="email"
    def email_addr(self,email):
        # 清空再输入
        self.clear((By.NAME, 'email'))
        self.send_key((By.NAME, 'email'),email)

    # TODO 定位：邮政编码：name = "zipcode"
    def postal_code(self,post_num):
        self.send_key((By.NAME, 'zipcode'),post_num)

    # TODO 定位：手机：name="mobile"
    def mobile_phone(self,phone_number):
        self.send_key((By.NAME, 'mobile'),phone_number)

    # TODO 定位：新增收货地址 按钮：name="submit"
    def add_receiving_address(self):
        self.click((By.NAME, 'submit'))

    # TODO 定位：删除 按钮 XPATH:/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[2]
    def del_addr(self):
        self.click((By.XPATH,'/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[2]'))

    # TODO 确认弹框选择，alert
    def choice_alert(self):
        # 获取弹框对象
        alert = self.driver.switch_to.alert
        # 点击：确认
        alert.accept()

if __name__ == '__main__':
    driver = browser_operation()
    login = LoginPage(driver)
    addr = ReceivingAddressPages(driver)
    login.get_website('http://172.16.140.55/ecshop/user.php')
    login.input_username('豆芽纸')
    login.input_password('123456')
    login.btn_login()


    # 调用：新增收货地址流程
    addr.implicitly_wait(5)

    addr.btn_receiving_address()
    addr.select_province('湖北省')
    addr.select_city('武汉市')
    addr.select_district('洪山区')
    addr.consignee_name('豆芽纸')
    addr.detailed_addr('湖北省武汉市洪山区曙光星城A区')
    addr.telephone('027-28865253')
    addr.email_addr('646785213@qq.com')
    addr.postal_code('410009')
    addr.mobile_phone('12412345678')
    # 增加地址
    addr.add_receiving_address()
    time.sleep(2)

    # 删除地址
    addr.del_addr()
    addr.choice_alert()
    time.sleep(2)
    addr.quit(2)
